package jdbcTest;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

public class StudentDao {
	private Connection conn;
	
	public boolean addStudent(Student stu)throws SQLException,ClassNotFoundException{
		if(conn==null){
			conn=ConnectionUtil.getConnection();
		}
		Statement stmt=conn.createStatement();
		String sqlString=String.format("insert into crn values('%s','%s','%s')",
				stu.getXh(),stu.getXm(),stu.getXb());
				int i=stmt.executeUpdate(sqlString);
		return i>0;
	}
	public boolean delStudent(Student stu)throws SQLException,ClassNotFoundException{
		if(conn==null){
			conn=ConnectionUtil.getConnection();
		}
		Statement stmt=conn.createStatement();
		String sqlString="delete from crn where xh='"+stu.getXh()+"'";
				int i=stmt.executeUpdate(sqlString);
		return i>0;
	}
	public boolean updateStudent(Student stu)throws SQLException,ClassNotFoundException{
		if(conn==null){
			conn=ConnectionUtil.getConnection();
		}
		Statement stmt=conn.createStatement();
		String sqlString="update crn set xh='"+stu.getXh()+"',xm="+stu.getXm()+"" +
				"',xb='"+stu.getXb()+"')";
				int i=stmt.executeUpdate(sqlString);
		return i>0;
	}
	public ArrayList<Student> listAllArrayList()throws SQLException,ClassNotFoundException {
		Connection conn=ConnectionUtil.getConnection();
		Statement stmt=conn.createStatement();
		ResultSet rs=stmt.executeQuery("select * from crn;");
		ArrayList<Student> list=new ArrayList<Student>();
		while (rs.next()) {
			Student student=new Student();
			student.setXh(rs.getString("xh"));
			student.setXm(rs.getString("xm"));
			student.setXb(rs.getString("xb"));
			list.add(student);			
		}
		return list;
	}
}	